package com.dongdong.bookstore.module.ums.controller;


import com.dongdong.bookstore.common.exception.Asserts;
import com.dongdong.bookstore.module.ums.model.ResourceCategory;
import com.dongdong.bookstore.module.ums.service.ResourceCategoryService;
import com.dongdong.bookstore.module.ums.viewmodel.ResourceCategoryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 资源分类表  前端控制器
 * </p>
 *
 * @author chiangkai
 * @since 2021-10-20
 */
@Api(tags = "资源分类")
@RestController
@RequestMapping("/ums/resourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    @ApiOperation("查询所有后台资源分类")
    @GetMapping("/listAll")
    public List<ResourceCategory> listAll() {
        return resourceCategoryService.listAll();
    }

    @ApiOperation("添加后台资源分类")
    @PostMapping("/create")
    public String create(@RequestBody ResourceCategoryParam param) {
        boolean success = resourceCategoryService.create(param);
        Asserts.isFalse(success, "添加资源分类失败");
        return "添加资源分类成功";
    }

    @ApiOperation("修改后台资源分类")
    @PostMapping("/update/{id}")
    public String update(@PathVariable Long id,
                         @RequestBody ResourceCategoryParam resourceCategoryParam) {

        boolean success = resourceCategoryService.update(id,resourceCategoryParam);
        Asserts.isFalse(success, "更新失败");
        return "更新成功";
    }

    @ApiOperation("根据ID删除后台资源")
    @PostMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        boolean success = resourceCategoryService.removeById(id);
        Asserts.isFalse(success, "删除失败");
        return "删除成功";
    }
}
